package com.zl_huang.example.dao;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.zl_huang.example.model.Test;
@Repository
public class TestDaoImpl extends BaseHibernateDAO<Test>  implements ITestDao {
	@Autowired
	private SessionFactory sessionFactory;
	
	@Transactional
	public Session getSession(){
		return this.sessionFactory.getCurrentSession();
	}
    
	@Override
	@Transactional
	public Integer createTest(Test test) throws Exception {
		Session session = getSession();
		session.save(test);
		return 1;
	}

	@Override
	@Transactional
	public Integer editTest(Test test) throws Exception {
		Session session = getSession();
		session.update(test);
		return 1;
	}

	@Override
	@Transactional
	public Integer deleteTest(Integer id) throws Exception {
		Session session = getSession();
		Test test = (Test) session.get(Test.class, id);
		if(test != null){
			session.delete(test);
			return 1;
		}else{
			return 0;
		}
		
	}

	@Override
	@Transactional
	public List<Test> findAllTest() throws Exception {
		Session session = getSession();
		String hql = "from Test";
		@SuppressWarnings("unchecked")
		List<Test> testList = session.createQuery(hql).list();
		return testList;
	}

	@Override
	@Transactional
	public Test getTestById(Integer id) throws Exception {
		Session session = getSession();
		return (Test) session.get(Test.class, id);
		
	}

}
